using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace OpenEdge.Utility
{
	public class InterfaceSelectDialog : System.Windows.Forms.Form
	{

		public delegate void OKRequest(String packageName, int packageID);

		public event OKRequest			OKEvent;

		private EA.Repository repository;

		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TreeView treeView1;
		private System.Windows.Forms.ImageList imageList1;
		private System.ComponentModel.IContainer components;

		public InterfaceSelectDialog(EA.Repository repository)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//

			this.repository = repository;

			foreach (EA.Package currentPackage in repository.Models)
			{
				TreeNode newNode = new TreeNode(currentPackage.Name);
				this.treeView1.Nodes.Add(newNode);
				newNode.ImageIndex = 0;
				newNode.SelectedImageIndex = 0;
				newNode.Tag = currentPackage.PackageID;

				addChildPackages(newNode, currentPackage.Packages);
				addChildElements(newNode, currentPackage.Elements);

			}

			this.treeView1.BeforeExpand += new TreeViewCancelEventHandler(treeView1_BeforeExpand);

		}

		public void addChildPackages(TreeNode node, EA.Collection packages)
		{
			foreach(EA.Package currentPackage in packages)
			{
				TreeNode newNode = new TreeNode(currentPackage.Name);
				node.Nodes.Add(newNode);
				newNode.ImageIndex = 0;
				newNode.SelectedImageIndex = 0;
				newNode.Tag = currentPackage.PackageID;
			}

		}

		public void addChildElements(TreeNode node, EA.Collection elements)
		{
			foreach (EA.Element currentElement in elements)
			{
				if (currentElement.Type != "Interface")
					continue;
				TreeNode newNode = new TreeNode(currentElement.Name);
				node.Nodes.Add(newNode);
				newNode.ImageIndex = 1;
				newNode.SelectedImageIndex = 1;
				newNode.Tag = currentElement.ElementID;

			}

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(InterfaceSelectDialog));
            this.button2 = new System.Windows.Forms.Button();
            this.button1 = new System.Windows.Forms.Button();
            this.treeView1 = new System.Windows.Forms.TreeView();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.SuspendLayout();
            // 
            // button2
            // 
            this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.button2.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button2.Location = new System.Drawing.Point(248, 344);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(88, 23);
            this.button2.TabIndex = 5;
            this.button2.Text = "Cancel";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button1
            // 
            this.button1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.button1.Location = new System.Drawing.Point(144, 344);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(88, 23);
            this.button1.TabIndex = 4;
            this.button1.Text = "OK";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // treeView1
            // 
            this.treeView1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.treeView1.HotTracking = true;
            this.treeView1.ImageIndex = 0;
            this.treeView1.ImageList = this.imageList1;
            this.treeView1.Location = new System.Drawing.Point(8, 8);
            this.treeView1.Name = "treeView1";
            this.treeView1.SelectedImageIndex = 0;
            this.treeView1.Size = new System.Drawing.Size(328, 320);
            this.treeView1.TabIndex = 3;
            // 
            // imageList1
            // 
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
            this.imageList1.Images.SetKeyName(0, "");
            this.imageList1.Images.SetKeyName(1, "");
            this.imageList1.Images.SetKeyName(2, "");
            // 
            // InterfaceSelectDialog
            // 
            this.AcceptButton = this.button1;
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 16);
            this.CancelButton = this.button2;
            this.ClientSize = new System.Drawing.Size(346, 384);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.treeView1);
            this.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
            this.Name = "InterfaceSelectDialog";
            this.Text = "Select Interface";
            this.TopMost = true;
            this.ResumeLayout(false);

		}
		#endregion

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.OKEvent(this.treeView1.SelectedNode.Text, (int)this.treeView1.SelectedNode.Tag);
			this.Close();
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
		{
			foreach(TreeNode childNode in e.Node.Nodes)
			{
				EA.Package currentPackage = null;
				EA.Element currentElement = null;

				if (childNode.Nodes.Count > 0)
					continue;

				if (childNode.ImageIndex == 0)
				{
					try 
					{
						currentPackage = (EA.Package) this.repository.GetPackageByID((int) childNode.Tag);
					}
					catch(Exception exception)
					{
                        MessageBoxButtons buttons = MessageBoxButtons.OK;
                        MessageBox.Show(exception.Message, "EA Exception", buttons);
					}
					if (currentPackage != null)
					{
						addChildPackages(childNode, currentPackage.Packages);
						addChildElements(childNode, currentPackage.Elements);
					}
				}
				else
				if (childNode.ImageIndex > 0)
				{
					try 
					{
						currentElement = (EA.Element) this.repository.GetElementByID((int) childNode.Tag);
					}
					catch(Exception exception)
					{
                        MessageBoxButtons buttons = MessageBoxButtons.OK;
                        MessageBox.Show(exception.Message, "EA Exception", buttons);
					}
					if (currentElement != null)
					{
						addChildElements(childNode, currentElement.Elements);
					}
				}

			}
		}
	}
}
